import dayjs from 'dayjs';
import { downLoad, request, upload } from '../request';

/** get role list */
export function fetchGetRoleList(params?: Api.SystemManage.RoleSearchParams) {
  return request<Api.SystemManage.RoleList>({
    url: '/systemManage/getRoleList',
    method: 'get',
    params
  });
}

/**
 * get all roles
 *
 * these roles are all enabled
 */
export function fetchGetAllRoles() {
  return request<Api.SystemManage.AllRole[]>({
    url: '/systemManage/getAllRoles',
    method: 'get'
  });
}

/** get user list */
export function fetchGetUserList(params?: Api.SystemManage.UserSearchParams) {
  return request<Api.SystemManage.UserList>({
    url: '/systemManage/getUserList',
    method: 'get',
    params
  });
}

/** get menu list */
export function fetchGetMenuList() {
  return request<Api.SystemManage.MenuList>({
    url: '/systemManage/getMenuList/v2',
    method: 'get'
  });
}

/** get all pages */
export function fetchGetAllPages() {
  return request<string[]>({
    url: '/systemManage/getAllPages',
    method: 'get'
  });
}

/** get menu tree */
export function fetchGetMenuTree() {
  return request<Api.SystemManage.MenuTree[]>({
    url: '/systemManage/getMenuTree',
    method: 'get'
  });
}

// 对接royi
export function getRoyiRoleList(params: any) {
  return request({
    url: '/system/role/list',
    method: 'get',
    params
  });
}

// 获取菜单树
export function getRoyiMenuTreeSelect(params: any) {
  if (!params) {
    params = {};
  }
  params.sysType = 'cdl';
  return request({
    url: '/system/menu/treeselect',
    method: 'get',
    params
  });
}

// 新增royi角色
export function addRoyiRole(data: any) {
  return request({
    url: '/system/role',
    method: 'post',
    data
  });
}

// 修改royi角色信息
export function editRoyiRole(data: any) {
  return request({
    url: '/system/role',
    method: 'put',
    data
  });
}

// 删除若依角色
export function delRoyiRole(roleId: string) {
  return request({
    url: `/system/role/${roleId}`,
    method: 'delete'
  });
}

// 获取royi角色详情
export function getRoyiRoleById(roleId: string) {
  return request({
    url: `/system/role/${roleId}`,
    method: 'get'
  });
}

// 根据角色ID查询菜单下拉树结构
export function roleMenuTreeselect(roleId: string) {
  let params = {
    sysType: 'cdl',
    roleId: roleId
  };
  return request({
    url: `/system/menu/roleMenuTreeselect`,
    method: 'get',
    params
  });
}

// 获取角色部门下拉树
export function getRoyiDepTree(roleId: string) {
  return request({
    url: `/system/role/deptTree/${roleId}`,
    method: 'get'
  });
}

// 修改角色状态
export function changeRoyiRoleStatus(roleId: string, status: string) {
  const data = {
    roleId,
    status
  };
  return request({
    url: '/system/role/changeStatus',
    method: 'put',
    data
  });
}

// 分配数据权限
export function royuDataScope(data: any) {
  return request({
    url: '/system/role/dataScope',
    method: 'put',
    data
  });
}

// 按角色id获取用户
export function getRoyiRoleUser(params: any) {
  return request({
    url: `/system/role/authUser/allocatedList`,
    method: 'get',
    params
  });
}

// 获取角色未授权的用户列表
export function royiUnallocatedList(params: any) {
  return request({
    url: '/system/role/authUser/unallocatedList',
    method: 'get',
    params
  });
}

// 取消用户授权角色
export function royiAuthUserCancel(data: any) {
  return request({
    url: '/system/role/authUser/cancel',
    method: 'put',
    data
  });
}

// 批量取消用户授权角色
export function royiAuthUserCancelAll(data: any) {
  return request({
    url: '/system/role/authUser/cancelAll',
    method: 'put',
    params: data
  });
}

// 授权用户选择
export function royiAuthUserSelectAll(data: any) {
  return request({
    url: '/system/role/authUser/selectAll',
    method: 'put',
    params: data
  });
}

// 导出角色列表
export function exportRoyiRole(data: any) {
  return downLoad({
    url: '/system/role/export',
    method: 'POST',
    data,
    fileName: `角色导出${dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`
  });
}

// 获取部门树
export function getRoyiDeptTree() {
  return request({
    url: '/system/user/deptTree',
    method: 'get'
  });
}

// 查询用户列表
export function getRoyiListUser(params: any) {
  return request({
    url: '/system/user/list',
    method: 'get',
    params
  });
}
// 删除用户
export function delRoyiUser(userId: string) {
  return request({
    url: `/system/user/${userId}`,
    method: 'delete'
  });
}

// 获取用户详情
export function getRoyiUserMsg(userId: string) {
  return request({
    url: `/system/user/${userId}`
  });
}

// 导出用户模板
export function downRoyiUser(data: any) {
  return downLoad({
    url: '/system/user/export',
    method: 'POST',
    data,
    fileName: `用户导出${dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`
  });
}

// 系在用户导入模板
export function downloadUserTemplate() {
  return downLoad({
    url: 'system/user/importTemplate',
    method: 'POST',
    fileName: `用户导入模板.xlsx`
  });
}

export function uploadUserTemplate(updateSupport: number, file: any) {
  return upload({
    url: `/system/user/importData?updateSupport=${updateSupport}`,
    files: file
  });
}

// 新增用户
export function addRoyiUser(data: any) {
  return request({
    url: '/system/user',
    method: 'post',
    data
  });
}

// 编辑用户
export function eidtRoyiUser(data: any) {
  return request({
    url: '/system/user',
    method: 'put',
    data
  });
}

// 获取部门信息
export function getDeptData(params: any) {
  return request({
    url: '/system/dept/list',
    method: 'get',
    params
  });
}

// 新增部门
export function addDept(data: any) {
  return request({
    url: '/system/dept',
    method: 'post',
    data
  });
}

// 修改部门
export function updateDept(data: any) {
  return request({
    url: '/system/dept',
    method: 'put',
    data
  });
}

// 删除部门
export function delDept(deptId: string) {
  return request({
    url: `/system/dept/${deptId}`,
    method: 'delete'
  });
}

// 获取岗位
export function getPostLit(params: any) {
  return request({
    url: '/system/post/list',
    method: 'get',
    params
  });
}

// 新增岗位
export function addPost(data: any) {
  return request({
    url: '/system/post',
    method: 'post',
    data
  });
}

// 修改岗位
export function updatePost(data: any) {
  return request({
    url: '/system/post',
    method: 'put',
    data
  });
}

// 删除岗位
export function delPost(postId: any) {
  return request({
    url: `/system/post/${postId}`,
    method: 'delete'
  });
}

// 导出岗位
export function exportPost(data: any) {
  return downLoad({
    url: 'system/post/export',
    method: 'POST',
    data,
    fileName: `岗位导出${dayjs().format('YYYY-MM-DD HH:mm:ss')}.xlsx`
  });
}

// 用户密码重置
export function resetUserPwd(userId: any, password: any) {
  const data = {
    userId,
    password
  };
  return request({
    url: '/system/user/resetPwd',
    method: 'put',
    data
  });
}

/** 获取任务中心列表 */
export function getHomeTaskList(params: any) {
  return request({
    url: '/qms/agv/notice/list',
    method: 'get',
    params
  });
}
export function noticelist() {
  return request({
    url: '/system/notice/list/nest',
    method: 'get'
  });
}
export function logininfordistinct() {
  return request({
    url: '/system/logininfor/login-day-count/distinct',
    method: 'get'
  });
}
export function equipmentcounttotalmap() {
  return request({
    url: '/mes/basicmodel/equipment/count/total/map',
    method: 'get'
  });
}
export function logininforcount() {
  return request({
    url: '/system/logininfor/login-day-count',
    method: 'get'
  });
}
export function usercountUser() {
  return request({
    url: '/system/user/count-user',
    method: 'get'
  });
}
export function basicmodelequipment() {
  return request({
    url: '/mes/basicmodel/equipment/count/map',
    method: 'get'
  });
}
export function logininfortimePeriod() {
  return request({
    url: '/system/logininfor/login-day-count/time-period',
    method: 'get'
  });
}

export function logininfordaycount() {
  return request({
    url: '/system/logininfor/login-day-count/distinct/time-period',
    method: 'get'
  });
}
